/*
 * Copyright (C) 2007 Evan Long    longx223 (at) umn (dot) edu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/**
 * @file   image_proc.c
 * @author Evan Long
 * 
 * @brief  Implementation of image processing functions.
 * 
 * 
 */

#include <stdio.h>
#include "image_proc.h"

point_t find_brightest_point(unsigned char* image, int width, int height)
{
  point_t brightest;
  brightest.x = -1;
  brightest.y = -1;

  pixel_24RGB_t* pixel = (pixel_24RGB_t*)image;
  int i,j;
  for(i=0; i<height; i++)
  {
    for(j=0; j<width; j++)
    {
      if(BRIGHTEST_THRESHOLD < pixel->red + pixel->green + pixel->blue)
      {
	//save_image(image, width, height, "out.ppm");
	brightest.x = j;
	brightest.y = i;
	return brightest;
      }
      pixel++;
    }
  }
  
  return brightest;
}

void save_image(unsigned char* image, int width, int height, char* filename)
{
  FILE* out = fopen(filename, "w");
  fprintf(out,"P6\n%d %d\n255\n",width,height);
  fwrite(image,height,3*width,out);
  fclose(out);
}
